﻿using System.IO;
using Moq;
using Sherwood.Content.UpdateServices.Attachments;
using Microsoft.VisualStudio.TestTools.UnitTesting;
using System;
using Sherwood.Content;
using Sherwood.Content.Contracts;
using System.Xml;
using Sherwood.Content.UpdateServices.Attachments.Tests.Properties;
using Sherwood.Content.UpdateServices.Tests;
using System.Linq;

namespace Sherwood.Content.UpdateServices.Attachments.Tests
{
    
    /// <summary>
    ///This is a test class for AttachmentXmlUpdaterTest and is intended
    ///to contain all AttachmentXmlUpdaterTest Unit Tests
    ///</summary>
    [TestClass()]
    public class AttachmentXmlUpdaterTest : MockedUpdateEnvironment
    {


        private TestContext testContextInstance;

        /// <summary>
        ///Gets or sets the test context which provides
        ///information about and functionality for the current test run.
        ///</summary>
        public TestContext TestContext
        {
            get
            {
                return testContextInstance;
            }
            set
            {
                testContextInstance = value;
            }
        }

        #region Additional test attributes
        // 
        //You can use the following additional attributes as you write your tests:
        //
        //Use ClassInitialize to run code before running the first test in the class
        //[ClassInitialize()]
        //public static void MyClassInitialize(TestContext testContext)
        //{
        //}
        //
        //Use ClassCleanup to run code after all tests in a class have run
        //[ClassCleanup()]
        //public static void MyClassCleanup()
        //{
        //}
        //
        //Use TestInitialize to run code before running each test
        //[TestInitialize()]
        //public void MyTestInitialize()
        //{
        //}
        //
        //Use TestCleanup to run code after each test has run
        //[TestCleanup()]
        //public void MyTestCleanup()
        //{
        //}
        //
        #endregion


        /// <summary>
        ///A test for FromXml
        ///</summary>
        public void FromXmlTestHelper<TAttachmentMeta>()
            where TAttachmentMeta : class , IAttachmentMeta, new()
        {
            var target = new AttachmentXmlUpdater<TAttachmentMeta>(MetaRepository.Object,
                                                                   new TextContentOnlyAttachmentPersister
                                                                       <TAttachmentMeta>(MetaRepository.Object));
            
            var tempFile = Path.Combine(Path.GetTempPath(), Guid.NewGuid().ToString()) + ".pdf";
            using (var fs = new FileStream(tempFile, FileMode.Create, FileAccess.Write))
                fs.Write(Resources.Test, 0, Resources.Test.Length);

            var xml = @"<meta><attachment url=""" + tempFile + @""" /></meta>";
            XmlReader reader = XmlReader.Create(new StringReader(xml));

            var meta = new TAttachmentMeta();
            MetaRepository
                .Setup(r => r.GetMeta<TAttachmentMeta>(It.IsAny<Guid>(), It.IsAny<string>(), It.IsAny<string>()))
                .Returns(meta);

            var expected = meta;
            IMeta actual = target.FromXml(Record.Object, Client.Object, string.Empty, UpdateMode.CreateOrUpdate, reader);
            Assert.AreEqual(expected, actual);

            Assert.AreEqual(1, expected.GetAttachments().Count());
            var attachment = expected.GetAttachments().First();
            Assert.AreEqual(Path.GetFileName(tempFile), attachment.FileName);
            Assert.AreEqual("This is a pdf document used to test text extraction with IFilter.", attachment.TextContent.Trim());

            File.Delete(tempFile);
        }

        [TestMethod()]
        public void FromXmlTest()
        {
            FromXmlTestHelper<MyAttachmentMeta>();
        }
    }
}
